import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_first/components/MTFoodItem.dart';

class MTBody extends StatefulWidget {
  const MTBody({Key? key, required this.list}) : super(key: key);
  final List list;
  @override
  _MTBodyState createState() => _MTBodyState();
}

class _MTBodyState extends State<MTBody> {
  int _activeIndex = 0;

  int getChildCount() {
    if (widget.list.length > 0) {
      return widget.list[_activeIndex]["foods"].length;
    }
    return 0;
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Color.fromRGBO(255, 245, 230, 1),
      width: double.infinity,
      height: double.infinity,
      child: Row(children: [
        Container(
          width: 120,
          color: Color.fromARGB(255, 255, 206, 206),
          child: ListView.separated(
              itemBuilder: (BuildContext context, int index) {
                return GestureDetector(
                  onTap: () {
                    setState(() {
                      _activeIndex = index;
                    });
                  },
                  child: Container(
                    color: _activeIndex == index
                        ? Colors.white
                        : const Color.fromARGB(255, 209, 206, 206),
                    height: 80,
                    alignment: Alignment.center,
                    child: Text(widget.list[index]["name"]),
                  ),
                );
              },
              separatorBuilder: (BuildContext context, int index) {
                return Container(
                  height: 1,
                  color: Colors.white,
                );
              },
              itemCount: widget.list.length),
        ),
        Expanded(
            child: ListView.builder(
          itemBuilder: (BuildContext context, int index) {
            return MTFoodItem(
              foodItem: widget.list[_activeIndex]["foods"][index],
            );
          },
          itemCount: getChildCount(),
        ))
      ]),
    );
  }
}
